const loadGruntTasks = require('load-grunt-tasks')
const sass = require('sass')

let config = {
  src: 'src',
  dest: 'dist',
  temp: '.temp',
  public: 'public',
  data: {}
}

try {
  const userConfig = require('./grunt.config.js')
  config = {...config, ...userConfig}
} catch (error) {
}


module.exports = grunt => {
  grunt.initConfig({
    // 清除编译文件
    clean: {
      main: [config.dest, config.temp]
    },
    // 编译 sass
    sass: {
      options: {
        implementation: sass
      },
      main: {
        files: [
          {
            expand: true,
            cwd: config.src,
            src: ['**/*.scss'],
            dest: config.temp,
            ext: '.css'
          }
        ]
      }
    },
    // 压缩 css
    cssmin: {
      main: {
        files: [
          {
            expand: true,
            cwd: config.temp,
            src: ['**/*.css'],
            dest: config.dest,
            ext: '.css'
          }
        ]
      }
    },
    // 转换ES6
    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: [
          {
            expand: true,
            cwd: config.src,
            src: ['**/*js'],
            dest: config.temp,
            ext: '.js'
          }
        ]
      }
    },
    // 压缩 js
    uglify: {
      main: {
        files: [
          {
            expand: true,
            cwd: config.temp,
            src: ['**/*.js'],
            dest: config.dest,
            ext: '.js'
          }
        ]
      }
    },
    // 编译 html 中 swig 模板语法
    'html_template': {
      main: {
        options: {
          locals: config.data
        },
        files: [
          {
            expand: true,
            cwd: config.src,
            src: ['**/*.html'],
            dest: config.temp,
            ext: '.html'
          }
        ]
      }
    },
    // 压缩 html
    htmlmin: {
      main: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
          minifyCSS: true,
          minifyJS: true
        },
        files: [{
          expand: true,
          cwd: config.temp,
          src: ['**/*.html'],
          dest: config.dest,
          ext: '.html'
        }]
      }
    },
    // 拷贝无需压缩的文件
    copy: {
      main: {
        files: [
          {
            expand: true,
            cwd: config.src,
            src: ['assets/fonts/*', 'assets/images/*', 'public'],
            dest: config.dest
          }
        ]
      }
    },
    // 监听文件变化
    watch: {
      sass: {
        files: [`${config.src}/**/*.scss`],
        tasks: ['sass']
      },
      html: {
        files: [`${config.src}/**/*.html`],
        tasks: ['html_template']
      },
      static: {
        files: [`${config.src}/assets/fonts/*`, `${config.src}/assets/images/*`, `${config.public}/*`]
      }
    },
    // 热更新服务
    browserSync: {
      dev: {
        bsFiles: {
          src: [config.temp]
        },
        options: {
          // 开启监听
          watchTask: true,
          server: {
            baseDir: [config.temp, config.public, config.src],
            routes: {
              '/node_modules': 'node_modules'
            }
          }
        }
      }
    }
  })

  loadGruntTasks(grunt)

  grunt.registerTask('build', '打包到目标目录', ['clean', 'sass', 'cssmin', 'babel', 'uglify', 'html_template', 'htmlmin', 'copy'])

  // watch 在 browserSync 之后
  grunt.registerTask('serve', '启动服务', ['sass', 'html_template', 'browserSync', 'watch'])
}